package com.zjw.interceptor;


import com.zjw.exception.SystemException;
import com.zjw.system.SystemReturnEnum;
import com.zjw.utils.JWTUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class JWTInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {

//        请求头中获取到token
        String token = request.getHeader("token");

//        调用工具类判断jwt是否合法
        if (JWTUtil.validateJWT(token)) {
//            合法放行
            return true;

        }
//        无效或非法响应
        throw new SystemException(SystemReturnEnum.NEED_LOGIN);
    }
}
